package vip.fuxk.blog.common;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.noear.snack.ONode;
import org.noear.snack.core.Feature;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;


@Data
public class R<T>  implements Serializable {

    public static final String ok_msg = "操作成功：）";
    public static final String error_msg = "操作失败：（";

    private T data;

    private String msg = ok_msg;

    private boolean success;

    private Long timestamp;

    private R(){
        this.timestamp = new Date().getTime();
    }

    public static <T> R<T> error(String msg){
        R r = new R();
        r.setMsg(msg);
        return r;
    }

    public   static <T> R<T> ok() {
        R r = new R();
        r.setMsg(ok_msg);
        r.setSuccess(true);
        return r;
    }

    public static <T> R<T> ok(T data) {
        R r = new R();
        r.setMsg(ok_msg);
        r.setSuccess(true);
        r.setData(data);
        return r;
    }

    public String toString(){
          return ONode.load(this,
                  Feature.PrettyFormat,
                  Feature.ArrayNullAsEmpty,
                  Feature.SerializeNulls,
                  Feature.ParseIntegerUseLong,
                  Feature.OrderedField
          ).toJson();
    }

}
